const {app, BrowserWindow, ipcMain} = require('electron')
const fs = require('fs')
const path = require("node:path");

const createWindow = () => {
    const win = new BrowserWindow({
        width: 1200,
        height: 800,
        webPreferences: {
            preload: path.join(__dirname, 'preload.js'),
        },
    })

    win.loadFile('index.html').then(r => console.log("载入成功")).catch(e => console.log(e))
    // win.loadURL(`https://be5.gpai.net`).then(r => console.log("载入成功")).catch(e => console.log(e))
    win.webContents.openDevTools()// 打开开发者工具
}

app.whenReady().then(() => {
    createWindow()
})

app.on('window-all-closed', () => {
    if (process.platform === 'darwin') {
        app.quit()
    }
})

app.on('activate', () => {
    if (BrowserWindow.getAllWindows().length === 0) {
        createWindow()
    }
})

// 监听渲染进程发来的消息
ipcMain.on('create-file', (event, data) => {
    ///Users/yx/Desktop
    let file = "/Users/yx/Desktop/helloElectron.txt"
    fs.writeFileSync(file, data, 'utf8');
    console.log(file);
});